//
//  HWGameLiveRankListController.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/11/16.
//

import UIKit
import JXSegmentedView
class HWGameLiveRankListController: HWBaseViewController {
    var tableView:UITableView!
    var tableViewProtocol = HWGameLiveRankListProtocol()
    var httpManager = HWGameLiveHttpManager()
    var type = 0
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        loadData()
        
        addRefresh()
        
    }

}

extension HWGameLiveRankListController {
    private func initUI() -> Void {
        self.navigationView.isHidden = true
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = UIColor.colorWithHexString(hexString: "#FFFFFF")
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -40
    }
    
    private func initFrame() -> Void {
        tableView.snp.makeConstraints { make in
            make.top.left.right.equalTo(0)
            if #available(iOS 11.0, *) {
                make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
            }else{
                make.bottom.equalTo(0)
            }
        }
    }
}

extension HWGameLiveRankListController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}

extension HWGameLiveRankListController {
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
//        tableView.mj_footer = MJRefreshBackNormalFooter()
//        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    func loadData() -> Void {
        tableView.mj_header?.endRefreshing()
        httpManager.getRankList(roomId: HWGameLiveImManager.shared.roomId, type: type) { models in
            print(models)
            self.tableViewProtocol.models = models
            self.tableView.reloadData()
        } failure: { error in
            
        }

    }
}
